package com.aruba.paging.paging.viewmodel;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.ViewModel;
import androidx.paging.LivePagedListBuilder;
import androidx.paging.PagedList;

import com.aruba.paging.entity.Movie;
import com.aruba.paging.paging.factory.MovieDataSourceFactory;
import com.aruba.paging.paging.model.MovieDataSource;

/**
 * Created by aruba on 2021/9/17.
 */
public class MovieViewModel extends ViewModel {
    public LiveData<PagedList<Movie>> pagedListLiveData;

    public MovieViewModel() {
        //配置项
        PagedList.Config config = new PagedList.Config.Builder()
                //最大加载多少条数据
                .setMaxSize(1000 * MovieDataSource.PER_PAGE)
                //首次加载多少条数据
                .setInitialLoadSizeHint(MovieDataSource.PER_PAGE * 2)
                //设置距离底部还有多少条数据时开始加载下一页的数据
                .setPrefetchDistance(2)
                //一页显示多少条
                .setPageSize(MovieDataSource.PER_PAGE)
                //设置控件占位
                .setEnablePlaceholders(false)
                .build();
        
        //通过Factory和Config配置
        pagedListLiveData = new LivePagedListBuilder<>(new MovieDataSourceFactory(), config)
                .build();
    }
}
